<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Page
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: Gmap.php 2010-08-31 16:05 idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Page
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Page_Api_Gmap extends Core_Api_Abstract
{
	protected $_ApiKey;
		
	public function getApiKey() 
	{
		if ($this->_ApiKey == null){
			$this->_ApiKey = Engine_Api::_()->getApi('settings', 'core')->getSetting('page.gmapkey');
		}
		
		return $this->_ApiKey;
	}
	
	public function validateAddress($address)
	{
		if (is_array($address)){
			$address = implode(',', $address);
		}
		
		return $address;
	}
	
	public function getMarker($address)
	{
		$address = $this->validateAddress($address);
		
		if ($address == ""){
			return array();
		}
		
		$address = rawurlencode($address);
		$url = $this->getGMapUrl($address);
		$marker = Engine_Api::_()->getDbTable('markers', 'page')->createRow();
		
		if ( ($result = file_get_contents($url)) != false ){
			$resultParts = explode(',',$result);
			if($resultParts[0] != 200){
	      return false;
			}
			
			$marker->latitude = $resultParts[2];
			$marker->longitude = $resultParts[3];
		}
		
		return $marker;
	}
	
	public function deleteMarker($page)
	{
		if (!($page instanceof Page_Model_Page)){
			throw new Exception('Wrong argument passed.');		
		}
		
		if (!$page->getIdentity()){
			throw new Exception('Wrong page does not exists.');
		}
		
		$markerTable = Engine_Api::_()->getDbTable('markers', 'page');
		$markerTable->delete("page_id = {$page->getIdentity()}");
		
		return true;
	}
	
	public function getMapBounds($markers)
	{
		$minLat = 200;
		$maxLat = -200;
		$minLng = 200;
		$maxLng = -200;
		
		if (count($markers) == 0){
			return array();
		}elseif (count($markers) == 1){
			$marker = $markers[0];
			$minLat = $maxLat = $marker['lat'];
			$minLng = $maxLng = $marker['lng'];
		}else{
			foreach($markers as $marker) {
				if (empty($marker['lng']) || empty($marker['lat'])) continue;
				
				if ($marker['lng'] <= $minLng) {$minLng = $marker['lng'];}
				if ($marker['lng'] >= $maxLng) {$maxLng = $marker['lng'];}
				if ($marker['lat'] <= $minLat) {$minLat = $marker['lat'];}
				if ($marker['lat'] >= $maxLat) {$maxLat = $marker['lat'];}
			}
		}
		
		if ($minLat == $maxLat && $minLng == $maxLng){
			$minLat -= 0.0009;
			$maxLat += 0.0009;
			$minLng -= 0.0009;
			$maxLng += 0.0009;
		}
		
		$mapCenterLat = (float)($minLat + $maxLat) / 2;
    $mapCenterLng = (float)($minLng + $maxLng) / 2;
    
    if ( $minLat == 200 || $maxLat == -200 || $minLng == 200 || $maxLng == -200 ) {
	    $minLat = '';
	    $maxLat = '';
	    $minLng = '';
	    $maxLng = '';
	    $mapCenterLat = '';
	    $mapCenterLng = '';
    }
    
		return array('min_lat' => $minLat, 'max_lat' => $maxLat, 'min_lng' => $minLng, 'max_lng' => $maxLng, 'map_center_lat' => $mapCenterLat, 'map_center_lng' => $mapCenterLng);
	}
	
	public function getGMapUrl($address) 
	{
		if ($address == ""){
			return "";
		}
		
		return 'http://maps.google.com/maps/geo?&q='.$address.'&output=csv&key='.$this->getApiKey();
	}
	
	public function getMapJS()
	{
		return '<script src="http://maps.google.com/maps?file=api&v=2&sensor=false&key='.$this->getApiKey().'" type="text/javascript"></script>';
	}
	
	public function getMarkers($paginator)
	{
		$markers = array();
		foreach( $paginator as $page ){
			if ($page->marker_id == null || $page->latitude == null || $page->longitude == null){
				continue;
			}
			$markers[] = array(
				'marker_id'=>$page->marker_id,
				'lat'=>$page->latitude,
				'lng'=>$page->longitude,
				'pages_id'=>$page->page_id,
				'pages_photo'=>$page->getPhotoUrl('thumb.normal'),
				'title'=>$page->getTitle(),
				'desc'=>substr($page->getDescription(),0,200),
			  'url' => $page->getHref()
			);
		}
		
		return $markers;
	}
	
}